<?php
namespace App\Http\Controllers\Product;

use App\Http\Controllers\Controller;
use App\Libraries\Decorator\Image\CoverDecorator;
use App\Libraries\Decorator\Image\ImagesDecorator;
use App\Libraries\Decorator\Image\NoImagesDecorator;
use App\Libraries\Decorator\Tool\IsEndDecorator;
use App\Models\Product\Product;
use App\Models\Product\ProductPurchase;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class ProductController extends Controller{


    protected $product;
    protected $product_detail;
    protected $cover_decorator;
    protected $images_decorator;
    protected $is_end_decorator;
    protected $no_images_decorator;

    public function __construct(Product $product, CoverDecorator $cover_decorator,
                                ImagesDecorator $images_decorator,
                                NoImagesDecorator $no_images_decorator,
                                IsEndDecorator $is_end_decorator)
    {
        $this->product = $product;
        $this->cover_decorator = $cover_decorator;
        $this->images_decorator = $images_decorator;
        $this->no_images_decorator = $no_images_decorator;
        $this->is_end_decorator = $is_end_decorator;
    }

    /**
     * 商城商品列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function products(Request $request){
        $validator = Validator::make($request->all(), [
            'page' => 'required|integer',
            'size' => 'required|integer',
            'category' => 'required|in:1,2',//1-正常 2-特殊商品
            'type' => 'in:1,2',//1-android 2-IOS
        ], [
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
            'category.required' => '10001',
            'category.in' => '10002',
            'type.in' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $this->product->addDecorator($this->cover_decorator);
        $this->product->addDecorator($this->no_images_decorator);
        $this->product->addDecorator($this->is_end_decorator);
        $data = $this->product->sellingProducts($request->page, $request->size, $request['category'], empty($request['type'])?1:$request['type']);
        return $this->sendSuccessResponse($data);
    }

    /**
     * 商品详细
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function product($id){
        $validator = Validator::make([
            'id' => $id,
        ], [
            'id' => 'required|integer',
        ], [
            'id.required' => '10001',
            'id.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $this->product->addDecorator($this->cover_decorator);
        $this->product->addDecorator($this->images_decorator);
        $data = $this->product->detail($id);
        return $this->sendSuccessResponse($data);
    }

    /**
     * 商品兑换历史
     * @param Request $request
     * @param ProductPurchase $product_purchase
     * @return \Illuminate\Http\JsonResponse
     */
    public function productPurchase(Request $request, ProductPurchase $product_purchase){
        $valid_data = $request->all();
        $validator = Validator::make($valid_data, [
            'page' => 'required|integer',
            'size' => 'required|integer',
        ], [
            'page.required' => '10001',
            'page.integer' => '10002',
            'size.required' => '10001',
            'size.integer' => '10002',
        ]);
        if ($validator->fails()) {
            return $this->sendFailResponse(400, $validator->errors()->first());
        }
        $product_purchase->addDecorator($this->cover_decorator);
        $product_purchase->addDecorator($this->is_end_decorator);
        $data = $product_purchase->get(Auth::id(), $request['page'], $request['size']);
        return $this->sendSuccessResponse($data);
    }
}